package scottb.flex;

import java.awt.*;

public class VGroupLayoutManager extends FlexLayoutManager {

    public Dimension preferredLayoutSize(Container container) {

        VGroup vgroup = (VGroup) container;

        Dimension[] childrenPreferredSizes = null;

        int gap = 0;

        if(null != vgroup.gap()) {
            gap = vgroup.gap();
        }

        int width = 0;

        if(vgroup.width() != null) {
            width = vgroup.width();
        }
        else {

            childrenPreferredSizes = getChildrenSizes(vgroup);   

            for(Dimension size : childrenPreferredSizes) {

                if(size.width > width) {
                    width = size.width;
                }
            }
        }     

        if(null != vgroup.paddingLeft()) {
            width += vgroup.paddingLeft();
        }

        if(null != vgroup.paddingRight()) {
            width += vgroup.paddingRight();
        }

        int height = 0;

        if(vgroup.height() != null) {
            height = vgroup.height();
        }
        else {

            if(null == childrenPreferredSizes) {
                childrenPreferredSizes = getChildrenSizes(vgroup);
            }  

            boolean multiple = false;

            for(Dimension size : childrenPreferredSizes) {

                height += size.height;

                if(multiple) {
                    height += gap;
                }
                else {
                    multiple = true;
                }
            }
        }     

        if(null != vgroup.paddingTop()) {
            height += vgroup.paddingTop();
        }

        if(null != vgroup.paddingBottom()) {
            height += vgroup.paddingBottom();
        }

        return new Dimension(width, height);
    }

    public void layoutContainer(Container container) {
      
        VGroup vgroup = (VGroup) container;

        int gap = 0;

        if(null != vgroup.gap()) {

            gap = vgroup.gap();
        }

        int marker = 0;

        if(null != vgroup.paddingTop()) {

            marker = vgroup.paddingTop();
        }

        for(Component childComponent : vgroup.getComponents()) {

            Dimension size = childComponent.getPreferredSize();
            int height = size.height;
            int width = size.width;

            int x = getX(vgroup, width);
            int y = marker;

            childComponent.setLocation(x, y);
            childComponent.setSize(width, height);
            childComponent.setBounds(x, y, width, height);

            marker += height;
            marker += gap;
        }
    }

    private int getX(VGroup vgroup, int width) {

        int x = 0;
        int paddingLeft = 0;
        int paddingRight = 0;

        if(null != vgroup.paddingLeft()) {
            paddingLeft = vgroup.paddingLeft();
        }

        if(null != vgroup.paddingRight()) {
            paddingRight = vgroup.paddingRight();
        }

        if(vgroup.align() != null) {

            if(vgroup.align() == HAlign.LEFT) {

                x = paddingLeft;
            }
            else if(vgroup.align() == HAlign.CENTER) {

                int vgroupWidth = vgroup.getWidth() - paddingLeft - paddingRight;

                x = vgroupWidth / 2 - width / 2;

                x += paddingLeft;
            }
            else if(vgroup.align() == HAlign.RIGHT) {

                x = vgroup.getWidth() - width - paddingRight;
            }
        }
        else {

            x = paddingLeft;
        }

        return x;
    }
}
